package database;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DatabaseDate extends DatabaseObject {

    public DatabaseDate() {
        value = new Date();
    }

    @Override
    public void setValue(Object value) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            this.value = (Date) sdf.parse((String) value);
        } catch(Exception e) {
            this.value = null;
        }
    }

    @Override
    public void setParam(String param, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object getParam(String param) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object getValue() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            return sdf.format((Date) value);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String toString() {
        return getValue().toString();
    }

    @Override
    protected void acceptValidator(Validator v) {
        v.validateDate(this);
    }

    @Override
    public int compareTo(DatabaseObject o) {
        try {
            Date d1 = (Date) value;
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
            Date d2 = (Date) sdf.parse((String) o.getValue());
            Long l1 = d1.getTime();
            Long l2 = d2.getTime();
            return l1.compareTo(l2);
        } catch (ParseException ex) {
            return 0;
        }
    }
}
